package cn.edu.scau.cmi.wuzhenzhou.adapter;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;

public class MyController implements Initializable{
	@FXML
	private Text schema1; // 适配器模式
	
	@FXML
	private TextArea area; // 结果输出区域
	
	@FXML
	private TextField myTextField1; // 名字输入的区域
	
	@FXML
	private TextField myTextField2; // 学号输入的区域
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		
	}
	
	public void changeSchema(ActionEvent event) { // 点击切换时的提示转换
		if(schema1.getText().equals("类适配器")) { // 显示单例时切换为多例模式
			schema1.setText("对象适配器");
		}else { // 显示多例时切换为单例模式
			schema1.setText("类适配器");
		}
		myTextField1.setText("");
		myTextField2.setText("");
		area.setText("");
	}
	
	int flag = 0;
	List<Student> students = new ArrayList<>();
	AdapterOfClass adapterOfClass = new AdapterOfClass();
	AdapterOfObject adapterOfObject = new AdapterOfObject();
	public void insertStudent(ActionEvent event) { // 增加学生
		String name = (String)myTextField1.getText();
		int number = Integer.valueOf(myTextField2.getText());
		for(int i = 0;i < students.size();i++) {
			if(students.get(i).getNumber() == number) {
				flag = 1;
			}
		}
		if(flag == 0) {
			Student student = new Student(number, name);
			students.add(student);
			if(schema1.getText().equals("类适配器")) {
				students = adapterOfClass.sort(students);
			}else {
				students = adapterOfObject.sort(students);
			}
			area.setText("");
			for(Student stu : students) {
				area.appendText(stu.toString() + "\n");
			}
		}
		if(flag == 1) {
			area.appendText("该学号已被注册\n");
			flag = 0;
		}
	}
}
